<?php 
class Score_model extends CI_Model
{
	public function __construct()
	{
		$this->load->database();
	}

	public function set_score()
	{	
		$a = file_get_contents('resource/js/test.json');
		// 把JSON字符串转成PHP数组
		$a = json_decode($a, true);
		//var_dump($a);
		$parm_1 = $this->input->post('parm_1');
		$parm_2 = $this->input->post('parm_2');
		$score = $a[0]['rule'][$parm_1][$parm_2];	
		$file = $_FILES["1"]["name"];
		$data = array(
			'parm1'=>$parm_1,
			'parm2'=>$parm_2,
			'score'=>$score,
			'remark'=>$this->input->post('remark'),
			'id'=>$_SESSION['user'],
			'file'=>$file
			);
		return $this->db->insert('score',$data);
	}

	public function write_score($data)
	{	
		$query = $this ->db ->get_where('score',array('record_id'=>$data['record_id']));
		$result = $query ->row_array();
		if(empty($result))
			return $this->db->insert('score',$data);
		else
		{
			return $this->db->update('score',$data,array('record_id'=>$data['record_id']));
		}
		
	}

	public function read_score($condition)
	{
		//读取表里面所有数据
		//$query = $this ->db->get('score');
		//return $query->result_array();
		$query = $this -> db-> get_where('score',array('id'=>$condition));
		return $query ->result_array();
	}

	public function read_score_by_id($record_id)
	{
		$query = $this -> db-> get_where('score',array('record_id'=>$record_id));
		return $query->row_array();
	}

	public function write_result($data)
	{	
		$exist = $this->db-> get_where('result',array('id'=>$data['id']));
		$exist = $exist->result_array();
		if(!empty($exist)){
			$update = array('total_score'=>$data['total_score'],'status'=>$data['status']);
			$this->db->update('result',$update,array('id'=>$data['id']));
		}
			
		else			
			return $this->db->insert('result',$data);
	}

	public function read_student($id)
	{
		$query = $this->db->get_where('student',array('id'=>$id));
		return $query->row_array();
	}

	public function delete($id)
	{
		return $this->db->delete('score',array('id'=>$id));
	}

	public function undate_result($data)
	{
		$update = array('verify_result'=>$data['verify_result']);
		$this->db->update('score',$update,array('record_id'=>$data['record_id']));
	}

	public function sum($relation)
	{		
		$a =$this->db->select_sum('score')->get_where('score',$relation);
		return $a->row_array();
	}

	public function find($id)
	{
		$a ='unchecked';
		$query = "select * from score where id=".$id." "." AND verify_result<>'unchecked'";
		//$data = $this->db->get_where('verify',array('id'=>$id));
		$data = $this->db->query($query);
		return $data->result_array();
	}

	// public function test()
	// {
	// 	$relation = "select count(record_id),score.id,student.name from score left join student on student.id=score.id group by student.id";
	// 	$data =  $this->db->query($relation);
	// 	return $data->result_array();
	// }

}
 ?>